package zrar.panel;

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JPanel;

import zrar.entity.AnswerPeople;
import zrar.entity.Prize;
import zrar.util.ToolUtil;
import zrar.util.SystemUtil;

public class DisplayPanel extends JPanel {
	
	private static List<AnswerPeople> peoples = new ArrayList<AnswerPeople>();
	private static List<Prize> prizes = new ArrayList<Prize>();
	
	private Prize prize;
	
	private static List<AnswerPeople> tempPeoples = new ArrayList<AnswerPeople>();
	
	private static final long serialVersionUID = 1L;	

	private boolean Gaming;
	
	private JLabel tis_lb;
	private JPanel tisPanel;//提示面板
	private JPanel runPanel;//滚动面板
	
	private Font font1 = new Font("宋体",Font.BOLD,26);
	private Font font_big = new Font("宋体",Font.BOLD,35);
	
	public DisplayPanel(){
		this.setBackground(Color.WHITE);
		this.setLayout(null);
		
		tis_lb = new JLabel(" ",JLabel.CENTER);
		
		tis_lb.setFont(font1);
		tis_lb.setForeground(Color.red);
		
		tisPanel = new JPanel();
		runPanel = new JPanel();
		
		init();
		
		tisPanel.add(tis_lb);
		this.add(tisPanel);
		this.add(runPanel);
	}
	
	
	
	private void init() {
		tisPanel.setBackground(Color.WHITE);
		runPanel.setBackground(Color.WHITE);
		tisPanel.setBounds(0,0,715,50);
		runPanel.setBounds(0,51,715,430);
		
	}



	public void moveLable(final List<AnswerPeople> tempPeoples) {
		try {
			EventQueue.invokeLater(new Runnable() {
				public void run() {					
					
					Component[] comps = runPanel.getComponents();
					for(int j=0;j<comps.length;j++){
						((JLabel)comps[j]).setText("");
					}
					for(int i=0;i<tempPeoples.size();i++){
						((JLabel)comps[i]).setText(displayContent(tempPeoples.get(i)));
					}
					
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String displayContent(AnswerPeople pp){
		String discon="";
		String pName=pp.getPeopleName();
		if(pName.length()>5){
			pName=ToolUtil.cutUpLongName(pName);
		}
		
		String head=pp.getPeoTel().substring(0, 3);
		String tail= pp.getPeoTel().substring(7,pp.getPeoTel().length());		
		discon=pName+head+"****"+tail;
				
		return discon;
	}

	class GameDriver implements Runnable {

		@Override
		public void run() {
			
			peoples=SystemUtil.getPeoplelist();
			
			int viewLabelnum = SystemUtil.getViewLabelNum();
			
			while (Gaming) {												
				List<Integer> intlist=ToolUtil.randomNums(viewLabelnum, peoples.size());
				tempPeoples.clear();
				for(int j=0;j<viewLabelnum;j++){
					tempPeoples.add(peoples.get(intlist.get(j)));
				}
				
				moveLable(tempPeoples);	
				try {
					Thread.sleep(SystemUtil.speed);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}				
			}
			
			//循环 显示停止后 保存 抽中奖的人
			prize = SystemUtil.getCurPrize();
			prize.getPersons().addAll(tempPeoples);
			
			
			peoples.removeAll(tempPeoples);			
			
			if(prize.getPersons().size()>=prize.getTotal()){
				prize.setIsFinish(true);
			}
		}
	}

	public void start() {
		
		Gaming = true;
		new Thread(new GameDriver()).start();
	}

	public void stop() {
		Gaming = false;
	}

	public boolean isRunning() {
		return Gaming;
	}
	
	public void removeAllinRunPanel(){
		runPanel.removeAll();
	}

	public void setLayoutInRun(LayoutManager mgr) {
		runPanel.setLayout(mgr);
	}


	public void addInRun(JLabel lab) {
		runPanel.add(lab);	
	}
	
	public void showInTisPanel(String info){
		
		Font tisFont =font1;
		
		if(info.length()<15){
			tisFont = font_big;
		}
		tis_lb.setFont(tisFont);
		tis_lb.setText(info);
	}
	
}
